(function() {
  
  function getScript(src) {
    document.write('<' + 'script src="' + src + '"' +
                   ' type="text/javascript"><' + '/script>');
  }
  getScript("http://maps.visicom.ua/api/2.0.0/map/world_ru.js");
})();

function isMarkerVisible() {
   return (marker.visible());
}

function fillUrls() {
   var markerlat = null;
   var markerlng = null;
   if (isMarkerVisible()) {
      markerlat = marker.coords()[0].lat;
      markerlng = marker.coords()[0].lng;
   }
   var maptype = 'map';
   fillUrlsCommon(map.center().lat, map.center().lng, map.zoom(), maptype, markerlat, markerlng);
}

function processMarker(isGPS) {
   if (isGPS) {
      if (marker.visible()) {
         marker.visible(false);
      }
      gps_coords = {lng: gps_lng, lat: gps_lat};
      marker.coords(gps_coords);
      marker.visible(true);
      map.center(gps_coords);
   } else {
      if (marker.visible()) {
         marker.visible(false);
      } else {
         marker.coords(map.center());
         marker.visible(true);
      }
   }
   fillUrls();
   markerButtons(isGPS);
   map.repaint();
}

function initialize() {

   initMapCanvasSize();

   var lat = getParameter('lat');
   if (lat == null || isNaN(lat))
      lat = def_lat;
   var lng = getParameter('lng');
   if (lng == null || isNaN(lat))
      lng = def_lng;
   var z = parseInt(getParameter('z'));
   if (z == null || isNaN(z))
      z = def_zoom;


 var mapCenter = {lng: lng, lat: lat};
   
   var mlat = getParameter('mlat');
   var mlng = getParameter('mlng');

   var markerCoordinates = null;
   if (mlat != null && !isNaN(mlat) && mlng != null && !isNaN(mlng)) {
      markerCoordinates = {lng: mlng, lat: mlat};
   }
   
   map = new VMap(document.getElementById('map_canvas'));
   map.center(mapCenter, z);


   // Create marker
   marker = new VMarker(markerCoordinates);

   marker.visible(markerCoordinates != null ? true : false);
   
   // Set draggable option
   marker.draggable(true);
   // The hint
   //marker.hint('Hint');
     
   // Set header and description of information window
   //marker.info('header', 'Description');
    
   // Add a marker on the map
   map.add(marker);
   // Repaint the map
   map.repaint();

   marker.enddrag(fillUrls);
   

   // init methods
   fillUrls();
   initMarkerButtons(marker.visible());
   pageOnLoad();

   map.enddrag(fillUrls);
   map.onzoomchange(fillUrls);
}

function resizeMap() {
   map.repaint();
   fillUrls();
}